/*
 * (c) Copyright 2008, 2009 Hewlett-Packard Development Company, LP
 * All rights reserved.
 * [See end of file]
 */

package com.hp.hpl.jena.clusteredtdb.comms;


import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;


import lib.Bytes;


public class Ping
{
    SocketAddress addr = null ;
    SocketChannel chann = null ;

    String message = "abcde" ;
    
    public Ping(String server)
    {
        try
        {
            addr = new InetSocketAddress(server, 3030) ;
            chann = SocketChannel.open() ;
            Socket sock = chann.socket() ;
            sock.connect(addr) ;
        } catch (IOException ex)
        {
            throw new CommsException(ex) ;
        }
        
        for ( int i = 0 ; i < 8 ; i++)
            // 5 chars to start with => 3.2K
            message = message+message ;
        
    }

    
    public void ping()
    {
        // Need a lightweight encoding scheme.
        // See hadoop Writables.
        {
//            byte[] b = Bytes.string2bytes(message) ;
//            NetBlock nBlk = NetBlock.alloc(b.length) ;
//            ByteBuffer bb = nBlk.getBuffer() ;
//            bb.put(b) ;
            NetBlock nBlk = NetBlock.alloc(2*message.length()) ;
            ByteBuffer bb = nBlk.getBuffer() ;
            Bytes.toByteBuffer(message, bb) ;
            
            NetBlock.write(chann, nBlk) ;
        }

        {
            NetBlock nBlk = NetBlock.read(chann) ;
            ByteBuffer bb = nBlk.getBuffer() ;
            String msg2 = Bytes.fromByteBuffer(bb) ;
            //String msg2 = Bytes.string(bb, bb.limit()) ;
            if (!  msg2.equals(message.toUpperCase())) 
	//	System.out.println("message:"+message);
          //      System.out.println("Client: ??: "+msg2) ;
                System.out.println("Server:     "+msg2);
        }
    }
    
    // Simple string to NetBlock.
    public static NetBlock block(String str)
    {
        byte[] b = Bytes.string2bytes(str) ;

        NetBlock nBlk = NetBlock.alloc(b.length) ;
        ByteBuffer bb = nBlk.getBuffer() ;
        bb.put(b) ;
        return nBlk ;
    }
    
    // -----------------------------------------------------------------
    
    public static void startServer()
    {
        PingServer ps = new PingServer() ;
        Thread th = new Thread(new Server(ps)) ;
        th.setDaemon(true) ;
        th.start() ;
        Thread.yield() ;
    }
    
    public static class PingServer implements Server.Dispatch
    {
        public void read(SelectionKey key)
        {
            SocketChannel channel = (SocketChannel)key.channel() ;
            try {
                NetBlock netBlock = NetBlock.read(channel) ;
                if ( netBlock == null )
                {
                    channel.close() ;
                    return ;
                }

                ByteBuffer bb = netBlock.getBuffer() ;
                //String m = Bytes.string(bb, bb.limit()) ;
                String m = Bytes.fromByteBuffer(bb) ;
                m = m.toUpperCase() ;
                System.out.println("PingServer: "+m);
                byte[] b = Bytes.string2bytes(m) ;


                NetBlock ret =  NetBlock.alloc(b.length) ;
                ret.getBuffer().put(b) ;
                NetBlock.write(channel, ret) ;
            } catch (IOException ex)
            {
                try { channel.close() ; } catch (IOException ex2) {}
            }
        }
    }
}

/*
 * (c) Copyright 2008, 2009 Hewlett-Packard Development Company, LP
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
